import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import edu.wpi.first.wpilibj.networktables.NetworkTable;


public class Main {
	public static final String[] POSSIBLE_DRIVER_STATION_IPS = { "10.6.75.05", "10.6.75.09" };
	public static final String CRIO_IP = "10.6.75.2";
	public static final int MY_PORT = 15000;
	public static final int DRIVER_STATION_PORT = 14000;
	
	private static DatagramSocket udpSocket;
    private static SocketAddress[] dsAddresses;
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		udpSocket = new DatagramSocket(MY_PORT);
		dsAddresses = new SocketAddress[POSSIBLE_DRIVER_STATION_IPS.length];
		for(int a = 0; a < dsAddresses.length; a++) {
			dsAddresses[a] = new InetSocketAddress(POSSIBLE_DRIVER_STATION_IPS[a], DRIVER_STATION_PORT);
		}
		
		NetworkTable.setClientMode();
        NetworkTable.setIPAddress(CRIO_IP);
        NetworkTable table = NetworkTable.getTable("SmartDashboard");
        TargetOverlord targetOverlord = new TargetOverlord();
		
		Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
		BufferedImage image = null;
		
		while(true) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}


			boolean imageAvailable = false;
			try {
				imageAvailable =  clipboard.isDataFlavorAvailable(DataFlavor.imageFlavor);
			} catch(IllegalStateException e) {
				e.printStackTrace();
			}
			

			System.out.println("Image state:" + imageAvailable);
			
			if(imageAvailable) {
				try {
					image = (BufferedImage) clipboard.getData(DataFlavor.imageFlavor);
				} catch (UnsupportedFlavorException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch(Exception e){
					e.printStackTrace();
				}
				
				table.putNumber("Image Width", image.getWidth());
				table.putNumber("Image Height", image.getHeight());
				ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
				DataOutputStream stream = new DataOutputStream(byteStream);
				for(int a = 0; a < image.getWidth(); a++) {
					stream.writeInt(a);
					for(int b = 0; b < image.getHeight(); b++) {
						int rgb = image.getRGB(a, b);
						byte red = (byte) ((rgb >> 16) & 0x000000FF);
						byte green = (byte) ((rgb >> 8 ) & 0x000000FF);
						byte blue = (byte) ((rgb) & 0x000000FF);
//						System.out.print("(" + red + ", " + green + ", " + blue + ") ");
						stream.writeByte(red);
						stream.writeByte(green);
						stream.writeByte(blue);
					}
					for(int b = 0; b < dsAddresses.length; b++) {
						DatagramPacket packet = new DatagramPacket(byteStream.toByteArray(), 0, byteStream.toByteArray().length, dsAddresses[b]);
						udpSocket.send(packet);
						}
					byteStream = new ByteArrayOutputStream();
					stream = new DataOutputStream(byteStream);
				}
				System.out.println();
			}
			
//            final NumberArray bfrCoords = new NumberArray();
//            table.retrieveValue("BFR_COORDINATES", bfrCoords);
//            targetOverlord.update(bfrCoords,table);
		}
	}

}
